define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;
    var echarts = require("dist/echarts/init.js");
    var pollser = null;
    var $item = $(".carousel-inner .item");
    $(".carousel-inner").height($("body").height() - 130);
    $("[data-toggle='echarts']").height($("body").height() - 220);
    $(".carousel-control").height($("body").height() - 220);
    $item.length && $item.each(function() {
        var $this = $(this),
            $echarts_list = $("[data-toggle='echarts']", $this),
            $table = $('table[data-table="ajaxpoll"]', $this);
        $echarts_list.length && $echarts_list.each(function() {
            poll($(this).attr("id"), $(this), $this);
        });
        $table.length && poll("", "", $this);
    });
    $('.carousel').carousel({
        interval: 10000
    });

    $('#carousel-example-generic').on('slide.bs.carousel', function(e) {
        return false;
    })
    $('#carousel-example-generic').on('slid.bs.carousel', function(e) {
        return false;
    });
    polls();
    var index = 0;

    function polls() {
        var $poll = $('#carousel-example-generic'),
            $item = $(".item", $poll),
            len = $item.length,
            times = polltime.labeltime || 10000;
        pollser = setInterval(function() {
            $item.each(function() {
                var $this = $(this),
                    left = parseFloat($this.css("left"));
                if (left > 0 || left < 0) {
                    $this.removeClass("active").css({
                        "z-index": 1
                    });
                }
            });
            index++;
            if (index == len) {
                index = 0;
            }
            if (index) {
                $item.eq(index - 1).removeClass("active").css({
                    left: "-100%"
                });
            } else {
                $item.eq(len - 1).removeClass("active").css({
                    left: "-100%"
                });
            }
            $item.eq(index).addClass("active").css({
                left: 0
            });

            $item.each(function(i) {
                var $this = $(this),
                    left = parseFloat($this.css("left"));
                if (left < 0) {
                    $this.css({
                        "left": -left + "px",
                        "z-index": -1
                    });
                }
            });

        }, times)
    }


    function cecharts(id, obj, tab) {
        if (id) {
            var path = obj.data("path"),
                title = obj.data("title");
            path && method.get(path, function(d) {
                if (config.issucceed(d)) {
                    if (d.threshold) {
                        d.data[0].markLine = {
                            "itemStyle": {
                                "normal": {
                                    "color": "#87cefa",
                                    "type": "solid"
                                }
                            },
                            "data": [
                                [{
                                    "name": "阈值",
                                    "value": d.threshold,
                                    "xAxis": 0,
                                    "yAxis": d.threshold
                                }, {
                                    "name": "阈值",
                                    "xAxis": d.data[0].xdate.length,
                                    "yAxis": d.threshold
                                }]
                            ]
                        }
                        d.data[1].markLine = {
                            "itemStyle": {
                                "normal": {
                                    "color": "#ff7f50",
                                    "type": "solid"
                                }
                            },
                            "data": [
                                [{
                                    "name": "阈值",
                                    "value": d.cthreshold,
                                    "xAxis": 0,
                                    "yAxis": d.cthreshold
                                }, {
                                    "name": "阈值",
                                    "xAxis": d.data[0].xdate.length,
                                    "yAxis": d.cthreshold
                                }]
                            ]
                        }
                    }

                    echarts.line(id, d.data, {
                        title: {
                            text: title,
                            subtext: d.subtext,
                            subtextStyle: {
                                "color": "#333",
                                "x": "center",
                                "fontSize": 14,
                                "fontWeight": "bold"
                            },
                        },

                        toolbox: {
                            show: false
                        }
                    });
                } else {
                    config.msg.error(d.message);
                }

            });
        } else {
            var $table = $('table[data-table="ajaxpoll"]', tab);
            if ($table.length) {
                var paths = $table.data("path")
                method.get(paths, function(d) {
                    if (config.issucceed(d)) {
                        $(".js_count_tr", $table).remove();
                        $table.append(template("table_count_temp", d));
                    } else {
                        config.msg.error(d.message);
                    }

                });
            }
        }


    }

    function poll(id, obj, tab) {
        cecharts(id, obj, tab);
        var times = polltime.ajaxtime || 10000;
        if (polltime.ajaxtime > 0) {
            setInterval(function() {
                cecharts(id, obj, tab);
            }, times);
        }
    }
    $(document).on("click", ".js_auto_scroll", function() {
        if ($(this).prop("checked")) {
            clearInterval(pollser);
            $(".carousel-control").removeClass("hide");
        } else {
            polls();
            $(".carousel-control").addClass("hide");
        }

    });
    $(document).on("click", ".carousel-control.right", function() {
        var $poll = $('#carousel-example-generic'),
            $item = $(".item", $poll),
            len = $item.length;
        $item.each(function() {
            var $this = $(this),
                left = parseFloat($this.css("left"));
            if (left > 0 || left < 0) {
                $this.removeClass("active").css({
                    "z-index": 1
                });
            }
        });
        index++;
        if (index == len) {
            index = 0;
        }
        if (index) {
            $item.eq(index - 1).removeClass("active").css({
                left: "-100%"
            });
        } else {
            $item.eq(len - 1).removeClass("active").css({
                left: "-100%"
            });
        }
        $item.eq(index).addClass("active").css({
            left: 0
        });

        $item.each(function(i) {
            var $this = $(this),
                left = parseFloat($this.css("left"));
            if (left < 0) {
                $this.css({
                    "left": -left + "px",
                    "z-index": -1
                });
            }
        });

    });
    $(document).on("click", ".carousel-control.left", function() {
        var $poll = $('#carousel-example-generic'),
            $item = $(".item", $poll),
            len = $item.length,
            active = $(".item.active", $poll).index();
        $item.each(function() {
            var $this = $(this),
                left = parseFloat($this.css("left"));
            if (left > 0 || left < 0) {
                $this.removeClass("active").css({
                    "z-index": 1
                });
            }
        });
        index--;
        if (active == 0 && index == 0) {
            index = len - 1;
        }
        if (index == -1) {
            index = len - 1;
        }
        if (index == len - 1) {
            $item.eq(0).removeClass("active").css({
                left: "100%"
            });
        } else {
            $item.eq(index + 1).removeClass("active").css({
                left: "100%"
            });
        }
        $item.eq(index).addClass("active").css({
            left: 0
        });
        $item.each(function(i) {
            var $this = $(this),
                left = parseFloat($this.css("left"));
            if (left > 0) {
                $this.css({
                    "left": -left + "px",
                    "z-index": -1
                });
            }
        });
    });
    var $marquee=$('[data-toggle="marquee"]');
    $marquee.length && (function(){
        var path=$marquee.data("path");
        marquees($marquee,path);
        var mtimes = polltime.ajaxtime || 10000;
        if (polltime.ajaxtime > 0) {
            setInterval(function() {
                marquees($marquee,path);
            }, mtimes);
        }
    })();
    function marquees(obj,path){
        method.get(path, function(d) {
            if (config.issucceed(d)) {
               obj.html(d.msg);
            } else {
                config.msg.error(d.message);
            }

        });
    }
});